<?php

namespace App\Http\Controllers\Home;

use App\Model\Branch;
use App\Model\Cate;
use App\Model\Order;
use App\Model\Order1;
use App\Model\OrderError;
use App\Model\PushOrder;
use App\Model\tel;
use App\Model\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;


class IndexController extends Controller
{
    public function index()
    {
        return view("home.index");
    }

    public function desktop(){
        return view("home.desktop");
    }

    public function aside(View $view){
        $user_cate = session('cate');
        $cate = DB::table("cate")
            ->whereRaw('pid=0')
            ->whereRaw('id in ('.$user_cate.')')
            ->get()->toArray();
        foreach ($cate as $k=>$v){
            $child = Cate::where("pid",$v->id)->get()->toArray();
            $cate[$k]->child = $child;
        }
        $view->with('cate',$cate);
    }

    public function inf(){


        $data = [
            "order" =>  "2022042603827",
            "orderno" => "104220426151515589874",
            "chargeaccount" =>  "13002373615",
            "order_status" =>  2,
            "finish_time" =>  time(),
        ];
        echo json_encode($data);
    }

    public function callbackTest(Request $request){
        $res  = $request->all();
        $info['ext'] = json_encode($res);
        OrderError::create($info);
        echo "success";
    }

    public function info(){
//          $url = 'http://asmile.cn:7006/order/2022041709250/18934642920/100/2.2.2.2';
//          $res = curlGet($url);
          $res1 = '{"code":-1,"msg":"未出码","url":"","oid":"2022041709250"}';
          $data = json_decode($res1);
          print_r($data->oid);
//        $xml = json_decode("{\"Order\":{\"OrderNo\":\"104211216191334508474\",\"OrderStatus\":\"\u672a\u5904\u7406\",\"BuyTime\":\"2021\/12\/16 19:13:17\",\"BuyNum\":\"2\",\"ProductId\":\"19011238\",\"ProductPrice\":\"95.0000\",\"ProductName\":\"DY-DYB-300\u5143-\u76f4\u5145-\u3010\u76f4\u51b2\u3011\",\"ProductType\":\"\u5728\u7ebf\u76f4\u5145\",\"TemplateId\":\"500056524\",\"ChargeAccount\":\"Xi.tong6\",\"ChargePassword\":{},\"ChargeGame\":{},\"ChargeRegion\":{},\"ChargeServer\":{},\"ChargeType\":\"4\",\"JSitid\":\"9218620\",\"GSitid\":\"9050973\",\"BuyerIp\":\"113.109.125.170\",\"OrderFrom\":{},\"RoleName\":{},\"RemainingNumber\":\"93.3400\",\"ContactType\":{},\"ContactQQ\":{},\"UseAccount\":{},\"CustomerOrderNo\":\"2339642988890336644\",\"ExtractSignId\":\"104211216191334508474\"}}");
//
//        $order = [];
//        $order['orderno'] = $xml->Order->OrderNo;
//        $order['status'] = $xml->Order->OrderStatus;
//        $order['buytime'] = strtotime($xml->Order->BuyTime);
//        $order['num'] = $xml->Order->BuyNum;
//        $order['productid'] = $xml->Order->ProductId;
//        $order['productprice'] = $xml->Order->ProductPrice;
//        $order['productname'] = $xml->Order->ProductName;
//        $order['producttype'] = $xml->Order->ProductType;
//        $order['templateid'] = $xml->Order->TemplateId;
//        $order['chargeaccount'] = $xml->Order->ChargeAccount;
////                    $order['chargetype'] = $xml->Order->ChargeType;
//        $order['chargetype'] = 2;
//        $order['jsitid'] = $xml->Order->JSitid;
//        $order['gsitid'] = $xml->Order->GSitid;
//        $order['buyerip'] = $xml->Order->BuyerIp;
//        preg_match('/\d+/',$order['productname'],$price);
//        $price = $price[0];
//        $orderPrice = $price;
//        $order['remainingnumber'] = $price * $order['num'];
//        $order['customerorderno'] = $xml->Order->CustomerOrderNo;
//        $order['extractsignid'] = $xml->Order->ExtractSignId;
//        $orderInfo = Order::create($order);
//
//        $price = $order['remainingnumber'];
//        $order['num'] = 1;
//
//        //是否拆分
//        switch ($price){
//            case 100:
//                PushOrder::create($order);
//                break;
//            case 200:
//                PushOrder::create($order);
//                break;
//            case 300:
//                PushOrder::create($order);
//                break;
//            case 500:
//                PushOrder::create($order);
//                break;
//            default:
////                $order['num'] = $xml->Order->BuyNum;
//                if($order['num'] > 1 && $orderPrice <= 500) {
//                    $order['remainingnumber'] = $orderPrice;
//                    $order['pid'] = $orderInfo->id;
//                    for ($i = 0; $i < $order['num']; $i++) {
//                        $order['orderno'] = $xml->Order->OrderNo . $i;
//                        PushOrder::create($order);
//                    }
//                }else{
//                    $order['remainingnumber'] = 500;
//                    $order['pid'] = $orderInfo->id;
//                    for ($i = 0;$i < $price / 500;$i++){
//                        $order['orderno'] = $xml->Order->OrderNo.$i;
//                        PushOrder::create($order);
//                    }
//                }
//
//        }
    }

    //统计下游当日订单
    public function getOrderInfo(Request $request){
        $get = $request->all();
        $id = $get['id'];

        $start_time=strtotime(date("Y-m-d",time() - 3600*24));
        $end_time = $start_time + 3600*24;
        $res = Order::where("bid",$id)->where("buytime",">",$start_time)->where("buytime","<",$end_time)->get();
        dd($res);
    }

    public function curlGet($url,$method,$auth,$merch)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'authorization: '.$auth,
                'merchantid: '.$merch,
                'authority: console-api-admin.fulu.com',
                'method: GET',
                'path: /api/Finance/GetBalance',
                'scheme: https',
                'origin: https://console.open.fulu.com',
                'referer: https://console.open.fulu.com/',
//                'token: 738e1b71-3084-4851-8ff1-b1c091fc1a7e'
            )
        );
        if ($method == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, 0);
        } elseif ($method == 'get') {
            curl_setopt($ch, CURLOPT_HEADER, 0);
        }

        curl_setopt($ch,CURLOPT_COOKIE,'sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221bf03c8d-5130-478f-a0e7-ce602f1eb884%22%2C%22first_id%22%3A%2217da3096494146-01fbc937d5dc2e6-978153c-3686400-17da3096495d79%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217da3096494146-01fbc937d5dc2e6-978153c-3686400-17da3096495d79%22%7D; logout=0; Qs_lvt_373066=1639579621; .AspNetCore.Cookies=CfDJ8EDM1kaH--ZOjCWshyipVPCGKfHZ23-0R3O6M9adnotaQdF6h41fy3KahiqnblkOC1MmRWIb1QAo-ZqF7XSLMVpJueFYSUeL-gRtxGyo_4ktweejvt0VhwtsoCxSCcUiFo-rkN40wCCtsk82RPVRCFTExUZB2YtUbFYxoGpfoWcQNMzJDKCG-KdaguL5XFf6lt_2wfgTkq33TLnxk7dSV3rom_sanv9lWwW_8d0mSHyAAC3ZoBE4xTjSmx6_ZURM_hRRiFvodTrW5YZ2a-ADMW_meYwCaZzYrqM40swz6IgyoCBaTsaAAsarR9jRxqLWshX_YL23JRTvVie2rNnCMBIsgCPsI2t48-3yF53zxKSpi_HDd8bvxxgfSQLCxkIZZwmO1rzoSJMkAZzBqv2HeK5bzaDmbOl0qMliI4F5C5MUJhl93M2D39HUDTGy7qgmaD9L4Snj3Y7jO1RsOEP74VBnntwzmV2oixPQm4Ihz9KlqDUollFr-umy6K7nHvX-9Eny1JQRygaAlY_EF5Q-POTaGuzrZxXKAvLnb0pWJwcjVcsU4625DiLcpfYuW7T0dKFq-I5NVWhFAqyBt6bShPLE1H5F_U0S9QUwOn5tETmc_9bDIJunMRapg6-EIjHRvQ6yZtQ9U5ROOa0bm1i85EHrgZ3mzglYy6Ay1W9_IoHQxDypAq4T2DBGhOd_0jqDYp6bZQ37Ro42wvU1uqeWO3wM1M9OhETTi63TNZ8mJ8GzAcsf9Jq6hezmxqVUJdD9OY1MWizAWLQIhNChCamz7TeKeIZCPw77Md8445w8NpP0wbYvEIi44Zo37K57SGwgI171xbBVPfDzM2tgrtIEky5eH8DzvJQ5JQv80G8BrieXuFQ-joXcfOW19JIDCm_I6-4sIfzlmoApHeQn5oYUX-QAFfQR5AVJvwWJ-Giti6LEXoU8rIbBnz5XBX1S4S0Y7Fmg62I58Q7iGmiSdO8ydSJVqlrunjJfxO8I9gURgfnKpDP80lES98JiaRKEBFXyK-LbD3R2uCP-_WKm-aj-CpTCmM_hpA7zgE3IVGrTIDH4MnPgUbD22i2QOvoAr9piua7CRNrloZ0hl5r-M8vGOyWJ2sKRrio2XV661hsw6eZFStctMgu4q-Qp_cGzTcJ05rrCcSt5ybfvIwHcgCOhUhSNztu36V4TSFkzDn1EEci8kl70PyCjgra6KAzoBMCPt6RjcyBy2Fz1lFW9uduZQLEn-dlssTY__yleqSE7D_Zfqt3V94OHT8DjCpq8r6SLFwJjKHbCiydt8_r1m6XChwGxXmPlwUWlwE0SrRwoxeGo7JcvxJPbpxS-rL9DbMTS6i9aoAyPsxSc6sM1zbFMtyjuMscECz33NfHS7JKh2w-mmrIeA-nTZesLyFKlIzY8N1w-s5Eno8sPA1ozJ0zD0Fc; Qs_pv_373066=896569930086710500%2C1418313215407966000%2C734216315935331500');

        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36");
//        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
//        curl_setopt($ch, CURLOPT_PROXYPORT, "2802");
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
